<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
class pengguna extends CI_Controller { 
    public function index(){ 
        $this->session->set_flashdata('message', 'Enter your id to auth your self'); 
        $this->load->view('login');
    }
    /*
     * login menggunakan mobile phone number dan password.
     * setiap password disimpan dam md5
     */
    public function dashboard(){ 
        $mobilenumber = $this->session->userdata('mobilephone');
        //berkas pengguna
        $query = $this->db->query('SELECT PB_IDBERKAS,PB_NAMABERKAS,PB_UKRNBERKAS,PB_TGLUGHBERKAS FROM BERKAS WHERE PB_NOHPPENGGUNA=?',array($mobilenumber));
        //history order print
        $query1 = $this->db->query('SELECT * FROM DETILTRANSAKSI a, BERKAS b WHERE a.PB_IDBERKAS=b.PB_IDBERKAS AND a.PB_STATUS!=\'CANCEL\' AND a.PB_NOHPPENGGUNA=?',array($mobilenumber));
        //seluruh transaksi printerbudi
        $query3 = $this->db->query('SELECT COUNT(*) AS TOTAL FROM DETILTRANSAKSI');
        //total pengguna printerbudi
        $query4 = $this->db->query('SELECT COUNT(*) AS TOTAL FROM PENGGUNA');
        //total retailer
        $query5 = $this->db->query('SELECT COUNT(*) AS TOTAL FROM RETAILER');
        //pending transaction
        $query6 = $this->db->query('SELECT COUNT(*) AS TOTAL FROM DETILTRANSAKSI WHERE PB_STATUS=\'PENDING\'');
        //size file
        $query7 = $this->db->query('SELECT SUM(PB_UKRNBERKAS) AS TOTAL FROM BERKAS WHERE PB_NOHPPENGGUNA=?',array($mobilenumber));
        //echo $query4->row()->TOTAL;
        //total transaksi pengguna
        $query8 = $this->db->query('SELECT COUNT(*) AS TOTAL FROM DETILTRANSAKSI WHERE PB_NOHPPENGGUNA=?',array($mobilenumber));
        //transaksi pengguna sukses
        $query9 = $this->db->query('SELECT COUNT(*) AS TOTAL FROM DETILTRANSAKSI WHERE PB_STATUS=\'SUCCESS\' AND PB_NOHPPENGGUNA=?',array($mobilenumber));
        //transaksi gagal
        $query10 = $this->db->query('SELECT COUNT(*) AS TOTAL FROM DETILTRANSAKSI WHERE PB_STATUS=\'CANCEL\' AND PB_NOHPPENGGUNA=?',array($mobilenumber));
        if($query && $query1){
            $data['daftarfile'] = $query->result();
            $data['daftarorder'] = $query1->result();
            $data['totaltransaksipb'] = $query3->row();
            $data['totalpenggunapb'] = $query4->row();
            $data['totalretailerpb'] = $query5->row();
            $data['totalongoingpb'] = $query6->row();
            $data['totalquotapb'] = $query7->row();
            $data['totaltpeng'] = $query8->row();
            $data['totaltsuk'] = $query9->row();
            $data['totaltcan'] = $query10->row();
            $this->load->view('part1-1');
            $this->load->view('part2-1');
            $this->load->view('part2-2',$data); 
            $this->load->view('part2-3');
            $this->load->view('part3-1');
        }
        else if(!$query){
            $this->load->view('part1-1');
            $this->load->view('part2-1');
            $this->load->view('part2-2'); 
            $this->load->view('part2-3');
            $this->load->view('part3-1');
        } 
    } 
    function login(){
        $mobilenumber = mysql_escape_string($this->input->post('mobilenumber'));
        $password = mysql_escape_string($this->input->post('password'));
        if (substr($mobilenumber,0,1) == '0' && is_numeric($mobilenumber)){
            $mobilenumber = "+62" . substr($mobilenumber,1);
            $password = hash('md5', $password) ;
        } 
        echo $mobilenumber."<+>".$password;
        //insertdatabasehere
        $query = $this->db->query('SELECT * FROM PENGGUNA WHERE PB_NOHPPENGGUNA=? AND PB_PASSWORD=?', array($mobilenumber,$password));

        if ($query->num_rows() == 1){
            $result = $query->row();
            if($result->PB_STATUSPENGGUNA == 'NOT VERIFIED'){
                $this->session->set_flashdata('message', 'Sorry, your cant login. Please verified your identity');
                redirect('/pengguna/index','refresh');
            }
            else{
                $record = array(
                          'mobilephone' => $mobilenumber,
                          'email' => $result->PB_EMAILPENGGUNA,
                          'nama' => $result->PB_NAMAPENGGUNA,
                          'avatar'=> crypt($result->PB_AVATARPENGGUNA, $salt) ,
                          'sessionid' => uniqid()
                );
                $this->session->set_userdata($record);
                $uniqid = uniqid('pb');
                $query = $this->db->query('INSERT INTO LOGIN_HISTORY VALUES(?,?,NOW(),\'SUCCESS\')',array($uniqid,$mobilenumber));
                redirect('/pengguna/dashboard');
            }
        }
        else{
            $this->session->set_flashdata('message', 'Sorry, your cant login. Please verified your identity');
            redirect('/pengguna/index','refresh');            
        }
    }
    
    function reg(){
            $this->load->view('part1-1');
            $this->load->view('part2-1'); 
            $this->load->view('part2-4');
            $this->load->view('part3-1');
    }
    
    function register(){
        $mobilenumber = $this->input->post('mobilenumber'); 
        $nama = $this->input->post('namapengguna');
        $email = $this->input->post('emailpengguna');
        $alamat = $this->input->post('alamatpengguna');
        $tgllahir = $this->input->post('tanggallahir');
        $password = hash('md5', $this->input->post('password'));
        $repassword = hash('md5', $this->input->post('repassword'));
        if($password!=$repassword){
            $this->session->set_flashdata('message','your password didn\'t match. Please try again ');
            redirect('/pengguna/reg','refresh');            
        }
        if(filter_var($email,FILTER_VALIDATE_EMAIL) && 
                is_numeric($mobilenumber) &&
                is_string($nama) && 
                is_string($alamat)){
            $dateregistered = getdate();
            if (substr($mobilenumber,0,1) == '0' && is_numeric($mobilenumber)){
                $mobilenumber = "+62" . substr($mobilenumber,1); 
            } 
            if(mkdir("./share/".$mobilenumber."/", 0700)){ 
                $query = $this->db->query('INSERT INTO PENGGUNA(PB_NOHPPENGGUNA,PB_NAMAPENGGUNA,PB_EMAILPENGGUNA,
                    PB_ALAMATPENGGUNA,PB_TGLLAHIR,PB_PASSWORD,PB_STATUSPENGGUNA,PB_REGISTERED,PB_STATUS) VALUES(?,?,?,?,?,?,\'NOT VERIFIED\',NOW(),\'ENABLED\')', 
                        array($mobilenumber,$nama,$email,$alamat,$tgllahir,$password));
                $redir = base_url().'index.php/pengguna/index/';
                $this->session->set_flashdata('message','success. continue to <a href="'.$redir.'">login</a>');
                redirect('/pengguna/reg','refresh');
            } 
            else{
                $this->session->set_flashdata('message','error occured');
                redirect('/pengguna/reg','refresh');
            }
        }
        else{
            $this->session->set_flashdata('message','Sorry, you cant register. Please make sure your input is valid');
            redirect('/pengguna/reg','refresh');
        }
    }
    function upload(){
        $config['upload_path'] = './share/'.$this->session->userdata('mobilephone').'/';
        $config['allowed_types'] = 'doc|png|jpg';
        $config['max_size'] = '10000';
        $config['remove_space'] = TRUE;
        $this->load->library('upload', $config); 
        if ($this->upload->do_upload()){
            $upload_result = $this->upload->data();
            $file_name  = str_replace(' ','', $upload_result['file_name']);
            $file_type  = $upload_result['file_type'];
            $raw_name  = str_replace(' ','', $upload_result['raw_name']);
            $orig_name  = str_replace(' ','', $upload_result['orig_name']);
            $file_ext  = $upload_result['file_ext'];
            $file_size  = $upload_result['file_size'];
            $is_image  = $upload_result['is_image'];
            $image_width  = $upload_result['image_width'];
            $image_height  = $upload_result['image_height'];
            $image_type  = $upload_result['image_type'];
            $image_size_str = $upload_result['image_size_str'];
            //echo $file_name."+".'./share/'.$this->session->userdata('mobilephone').'/';
            $uniqid = uniqid('pb');
            if($is_image==1){
                $query = $this->db->query('INSERT INTO BERKAS VALUES(?,?,?,NOW(),?,?,?,?,?,?,?)',array($uniqid,$this->session->userdata('mobilephone'),
                    $file_name,$file_size,'/share/'.$this->session->userdata('mobilephone').'/',$raw_name,$is_image,$image_width,$image_height,$image_size_str));
            }
            else if($is_image==0){
                $query = $this->db->query('INSERT INTO BERKAS() VALUES(?,?,?,NOW(),?,?,?,\'0\',\'0\',\'0\',\'0\')',array($uniqid,$this->session->userdata('mobilephone'),
                    $file_name,$file_size,'/share/'.$this->session->userdata('mobilephone').'/',$raw_name,$is_image));
            }
            else{
                $this->session->set_flashdata('message',$this->upload->display_errors());
                redirect('/pengguna/dashboard','refresh');                
            }
            $this->session->set_flashdata('message','your document '.$file_name.' is uploaded');
            redirect('/pengguna/dashboard','refresh'); 
        }
        else{
            $this->session->set_flashdata('message',$this->upload->display_errors());
            redirect('/pengguna/dashboard','refresh');
        }
    }
    function download($namefileencoded){
        $namefiledecoded = $namefileencoded;
        $fullfilename = '/share/'.$this->session->userdata('mobilephone').'/'.$namefileencoded;
        $data = file_get_contents($fullfilename);
        force_download($namefiledecoded, $data);  
    } 
    function order(){  
        $this->session->set_userdata('idberkas', $this->input->post('idberkas'));
        $this->load->view('order'); 
    }
    function orderprint(){
        $uniqid = uniqid('pb');
        $idretailer = 'sby01';
        $mobilephone = $this->session->userdata('mobilephone'); 
        $idberkas = $this->input->post('idberkas');
        $kertas = $this->input->post('optkertas');
        $warna = $this->input->post('optwarna');
        $kualitas = $this->input->post('optkualitas');
        $duplex = $this->input->post('duplex');
        $hlmncetak = $this->input->post('halamancetak');
        $copi = $this->input->post('copy');
        $tujuankirim = $this->input->post('tujuankirim');
        $tglkirim = $this->input->post('tglkirim');
        $fitur = $this->input->post('optfitur');
        $paket = '0';
        $note = $this->input->post('note');
        $orientation = $this->input->post('orientation');
        $query = $this->db->query('INSERT INTO DETILTRANSAKSI
            (PB_IDDTLTRANSAKSI,PB_IDRETAILER,PB_NOHPPENGGUNA,PB_IDBERKAS,PB_TGLDTLTRANSAKSI,PB_KERTAS,PB_WARNA,PB_KUALITAS,PB_DUPLEX,PB_HLMNCETAK,PB_COPI,PB_TUJUANKIRIM,PB_TGLKIRIM,PB_FITUR,PB_PAKET,PB_NOTE,PB_ORIENTATION,PB_STATUS) VALUES(?,?,?,?,NOW(),?,?,?,?,?,?,?,?,?,?,?,?,\'PENDING\')',
                array($uniqid,$idretailer,$mobilephone,$idberkas,$kertas,$warna,$kualitas,$duplex,$hlmncetak,$copi,$tujuankirim,$tglkirim,$fitur,$paket,$note,$orientation));
        if($query){
            $this->session->set_flashdata('message','your document will be printed ');
            redirect('/pengguna/dashboard','refresh');
        }
        else{
            $this->session->set_flashdata('message','error in transaction occured ');
            redirect('/pengguna/dashboard','refresh');
        }
        
    } 
    function cancelorder($idorder){
        $query = $this->db->query('UPDATE DETILTRANSAKSI SET PB_STATUS=\'CANCEL\' WHERE PB_IDDTLTRANSAKSI=?;',array($idorder));
        if($query){
            $this->session->set_flashdata('message','your order has been canceled ');
            redirect('/pengguna/dashboard','refresh');
        }
        else if(!$query){
            $this->session->set_flashdata('message','we cannot cancel your order ');
            redirect('/pengguna/dashboard','refresh');            
        }
    }
    function logout(){
        $this->session->unset_userdata('$record');
        $this->session->set_flashdata('message', 'yout have been logout');
        redirect('/pengguna/index','refresh'); 
    } 
    function forgotpassword(){ 
        $this->session->set_flashdata('message', 'yout have been logout');
        redirect('/pengguna/index','refresh');  
    }
    
    function profil(){
        $this->load->view('profil');
    }
    function file(){
        $mobilenumber = $this->session->userdata('mobilephone');
        //berkas pengguna
        $query = $this->db->query('SELECT PB_IDBERKAS,PB_NAMABERKAS,PB_UKRNBERKAS,PB_TGLUGHBERKAS FROM BERKAS WHERE PB_NOHPPENGGUNA=?',array($mobilenumber));        
        if($query){
            $data['daftarfile'] = $query->result();
            $this->load->view('part1-1');
            $this->load->view('part2-1'); 
            $this->load->view('part2-5',$data);
            $this->load->view('part3-1');
        }
        else{
            $this->load->view('part1-1');
            $this->load->view('part2-1'); 
            $this->load->view('part2-5');
            $this->load->view('part3-1');            
        }
    }
    function orderhistory(){
            $this->load->view('part1-1');
            $this->load->view('part2-1'); 
            $this->load->view('part2-6');
            $this->load->view('part3-1');
    }
}
?>
